package de.schlinke_marius.adapters;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import de.schlinke_marius.R;
import de.schlinke_marius.data.Example;

public class ExampleAdapter extends ArrayAdapter<Example> {
	private LayoutInflater inflater;
	private Context mContext;
	private static final String TAG = ExampleAdapter.class.getSimpleName();

	public ExampleAdapter(Context context, List<Example> examples) {
		super(context, R.layout.examplesview_listitem, R.id.tvExamplesListLabel,
				examples);
		this.inflater = LayoutInflater.from(context);
		this.mContext = context;
	}

	private static class ViewHolder {

		private ImageView iv_picture;
		private TextView tv_name;
		private TextView tv_price;
		private RatingBar rb_Benchmark;
		private Example example;

		public Example getExample() {
			return example;
		}
		
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;
		View view;

		if (convertView == null) {
			view = inflater.inflate(R.layout.examplesview_listitem, parent,	false);
			viewHolder = new ViewHolder();
			viewHolder.iv_picture = (ImageView) view.findViewById(R.id.ivExamplesList);
			viewHolder.tv_name = (TextView) view.findViewById(R.id.tvExamplesListLabel);
			viewHolder.tv_price = (TextView) view.findViewById(R.id.tvExamplesPrice);
			viewHolder.rb_Benchmark = (RatingBar) view.findViewById(R.id.rbExamplesList);
			view.setTag(viewHolder);

		} else {
			view = convertView;
			viewHolder = (ViewHolder) view.getTag();
		}
		initializeView(viewHolder, position);
		return view;
	}

	private void initializeView(ViewHolder viewHolder, int position) {
		viewHolder.example = getItem(position);
		viewHolder.tv_name.setText(viewHolder.getExample().getName());
		viewHolder.tv_price.setText(viewHolder.getExample().getPrice());
		Log.d(TAG, viewHolder.getExample().getBenchmark() + " ID: " + viewHolder.getExample().getId() + " Preis: " +viewHolder.getExample().getPrice());
		viewHolder.rb_Benchmark.setRating(Float.valueOf(viewHolder.getExample().getBenchmark()));
		viewHolder.iv_picture.setImageDrawable(viewHolder.getExample().getPicture());

		
	}
	

}
